//
//  HJFriendListHeaderView.m
//  KaiChat
//
//  Created by lx on 2023/12/21.
//

#import "HJFriendListHeaderView.h"
#import "HJFriendListCell.h"
#import "HJUnReadView.h"
#import "HJIM.h"


@interface HJFriendListHeaderCell()

@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) HJUnReadView *unReadView;


@end


@implementation HJFriendListHeaderCell

- (void)settingSubView {
    [super settingSubView];
    
    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.centerY.offset(0);
        make.width.height.offset(40);
    }];
 
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarImageView.mas_right).offset(10);
        make.centerY.offset(0);
        make.right.mas_lessThanOrEqualTo(-16);
    }];
    
    
    HJUnReadView *unReadView = [HJUnReadView addToView:self.contentView];
    unReadView.layer.backgroundColor = UIColor.tj_FF5100Color.CGColor;
    self.unReadView = unReadView;
    
    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    if ([_model.identifier isEqualToString:@"newFriend"]) {
        [_unReadView setNum:[_model.anyObject intValue]];
        _unReadView.tj_y = (self.contentView.tj_height - _unReadView.tj_height) * 0.5;
        _unReadView.tj_x = self.contentView.tj_width - _unReadView.tj_width - leftRightMargin;
    } else {
        _unReadView.frame = CGRectZero;
    }
}

- (void)setModel:(HJCellModel *)model {
    _model = model;
    _avatarImageView.image = [UIImage imageNamed:model.iconName];
    _nameLabel.text = model.text;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
  
}

- (void)setSessionModel:(HJChatSessionModel *)sessionModel {
    _sessionModel = sessionModel;
    [_avatarImageView tj_setImageWithUrl:sessionModel.sessionAvatar placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    _nameLabel.text = sessionModel.sessionName;
}
 

@end





@interface HJFriendListHeaderView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) HJCellModel *friendModel;



@end


@implementation HJFriendListHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

        [self addSubview:self.tableView];
        [self.tableView registerClass:HJFriendListHeaderCell.class forCellReuseIdentifier:@"HJFriendListHeaderCell"];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.rowHeight = 60;
        self.tableView.showsVerticalScrollIndicator = NO;
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.left.right.bottom.offset(0);
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUserApplyCountChangeNotification:) name:userApplyCountChangeNotification object:nil];
        [self loadData];
    }
    return self;
}


- (void)loadData {
    [HJIM.shareIM.userManager queryUserInfoWithUserId:HJUserInfo.userId complete:^(HJChatUserModel * _Nullable model) {
        dispatch_async_main_queue(^{
            self.friendModel.anyObject = model.applyCount;
            [self.tableView reloadData];
        });
    }];
}

- (void)handleUserApplyCountChangeNotification:(NSNotification *)not {
    NSInteger badgeValue = [not.object intValue];
    
    HJCellModel *model = self.friendModel;
    model.anyObject = @(badgeValue);
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJFriendListHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJFriendListHeaderCell" forIndexPath:indexPath];
    HJCellModel *model = self.datas[indexPath.row];
    cell.model = model;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
    HJCellModel *model = self.datas[indexPath.row];
    if (_didSelectRowAtIndexPathBlock) {
        _didSelectRowAtIndexPathBlock(model);
    }
}


- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        tableView.separatorColor = [UIColor tj_separatorColor];
        tableView.backgroundColor = [UIColor tj_backgroundColor];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.scrollEnabled = NO;
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedRowHeight = 0;
        if (@available(iOS 15.0, *)) {
            tableView.sectionHeaderTopPadding = 0;
        }
        _tableView = tableView;
    }
    return _tableView;
}


- (NSArray *)datas {
    if (!_datas) {

        NSMutableArray *array = @[
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"newFriend";
                model.text = @"New Friends";
                model.identifier = @"newFriend";
                self.friendModel = model;
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"group_icon";
                model.text = @"Groups";
                model.identifier = @"groups";
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"about_icon";
                model.text = @"About Kai";
                model.identifier = @"aboutKai";
            }],
        ].mutableCopy;
        
        if (isTestFight()) {
            [array insertObject:[HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                                        model.iconName = @"dapps_icon";
                                        model.text = @"DApps";
                                        model.identifier = @"DApps";
            }] atIndex:0];
        }
        _datas = array.copy;
    }
    return _datas;
}

- (void)dealloc {
    NSLog(@"%s", __func__);
    [[NSNotificationCenter defaultCenter] removeObserver:self name:userApplyCountChangeNotification object:nil];
}

@end

 

